<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="searchForm" ref="searchForm">
            <el-form-item prop="name">
              <el-input v-model="searchForm.name" :clearable="true" placeholder="员工名称"></el-input>
            </el-form-item>
            <el-form-item prop="phone">
              <el-input v-model="searchForm.phone" :clearable="true" placeholder="手机号"></el-input>
            </el-form-item>
            <el-form-item prop="depart">
              <el-select v-model="searchForm.depart" :clearable="true" placeholder="请选择部门">
                <el-option v-for="item in departList" :key="item.departId" :label="item.departName" :value="item.departName"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click="handleClickAdd()" size="mini"> 新增</el-button>
        </div>
      </el-col>
    </el-row>
    <el-row ref="tableRow">
      <el-table :data="tableList" :fit="true">
        <el-table-column type="index" label="序号" width="100" align="center"></el-table-column>
        <el-table-column prop="name" label="姓名" align="center"></el-table-column>
        <el-table-column prop="sex" label="性别" align="center">
          <template slot-scope="scope">
            <div>
              {{ scope.row.sex == 1 ? '男' : '女' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="手机号" align="center"></el-table-column>
        <el-table-column prop="company" label="公司" align="center"></el-table-column>
        <el-table-column prop="depart" label="部门" align="center"></el-table-column>
        <el-table-column prop="job" label="职位" align="center"></el-table-column>
        <!-- <el-table-column prop="createDate" label="创建时间"> </el-table-column> -->
        <!-- <el-table-column prop="linkMan" label="紧急联系人"  align="center"></el-table-column>
          <el-table-column prop="linkPhone" label="紧急联系电话"  align="center"></el-table-column> -->
        <el-table-column label="荣誉等级" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.employLevel == 1">黄金</span>
            <span v-else-if="scope.row.employLevel == 2">钻石</span>
            <span v-else>特等</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-row>
              <el-button v-has="permissionCheck" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
              <el-dropdown trigger="click" size="small">
                <el-button type="primary"><i class="fa fa-list-ul"></i>更多</el-button>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item v-has="permissionEdit" @click.native="handleClickEditor(scope.row)">编辑</el-dropdown-item>
                  <el-dropdown-item v-has="permissionDelete" @click.native="handleClickDelete(scope.row)">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-row>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>
<script>
import EmployApi from '@/api/system/employ';
import DepartApi from '@/api/system/depart';
import * as message from '@/constants/message';

export default {
  name: 'EmployManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      tableList: [],
      searchForm: {
        name: '',
        phone: '',
        depart: '',
      },
      departList: [],

      page: {
        pageNo: 1,
        pageSize: 20,
        pageTotal: 0,
      },
      permissionSearch: 'SYSTEM_MANAGER_SYS_EMPLOY_LIST_SEARCH',
      permissionCheck: 'SYSTEM_MANAGER_SYS_EMPLOY_LIST_SEE',
      permissionAdd: 'SYSTEM_MANAGER_SYS_EMPLOY_LIST_ADD',
      permissionEdit: 'SYSTEM_MANAGER_SYS_EMPLOY_LIST_EDIT',
      permissionDelete: 'SYSTEM_MANAGER_SYS_EMPLOY_LIST_DELETE',
    };
  },
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  watch: {},
  activated() {
    this.listTable();
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  methods: {
    init() {
      this.getDeparts();
      this.listTable();
      this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },


    // 获取所有的部门
    getDeparts() {
      DepartApi.listDepart().then((res) => {
        this.departList = res.data;
      });
    },
    handleClickAdd() {
      this.$router.push({ path: '/sys/employAdd' });
    },
    handleClickDetail(row) {
      this.$router.push({ path: '/sys/employDetail', query: { id: row.employId } });
    },
    handleClickEditor(row) {
      this.$router.push({ path: '/sys/employEdit', query: { id: row.employId } });
    },
    handleClickDelete(row) {
      this.$confirm(message.MSG_CONFIRM_DELETE, message.TITLE_PROMPT, {}).then(() => {
        EmployApi.deleteEmploy({ id: row.employId, state: -1 }).then((res) => {
          this.listTable();
          this.$message({
            type: 'success',
            message: message.MSG_ACTION_SUCCESS,
          });
        });
      });
    },
    handleSizeChange(size) {
      this.page.pageSize = size;
      this.listTable();
    },
    handleCurrentChange(val) {
      this.page.pageNo = val;
      this.listTable();
    },
    listTable() {
      let data = {
        pageNo: this.page.pageNo,
        pageSize: this.page.pageSize,
        name: this.searchForm.name,
        phone: this.searchForm.phone,
        depart: this.searchForm.depart,
      };
      // this.$emit('show-loading',true);
      EmployApi.listEmploy(data).then((res) => {
        this.tableList = res.data.list;
        this.page.pageTotal = res.data.total;
        //   this.$emit('show-loading',false);
      });
    },
    onSearch() {
      this.page.pageNo = 1;
      this.listTable();
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
  },
};
</script>
<style lang="scss" src="src/assets/css/views/content.scss" scope></style>
